package view;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import com.example.administrator.text.R;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import util.LogUtil;

/**
 * Created by liuxiaobin on 2017/4/17
 * 时间充值的View
 */

public class TimeRechargeLLayout extends LinearLayout{
    private Context mContext;
    private int mTime;
    private viewTimeRechargeOnClick mViewTimeRechargeOnClick;



    @Bind(R.id.view_timertcharge_iv1)   ImageView mImageView1;
    @Bind(R.id.view_timertcharge_iv2)   ImageView mImageView2;
    @Bind(R.id.view_timertcharge_iv3)   ImageView mImageView3;
    @Bind(R.id.view_timertcharge_iv4)   ImageView mImageView4;
    @Bind(R.id.view_timertcharge_view1) View mView1;
    @Bind(R.id.view_timertcharge_view2) View mView2;
    @Bind(R.id.view_timertcharge_view3) View mView3;

    public TimeRechargeLLayout(Context context) {
        super(context);
        init();
    }

    public TimeRechargeLLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        getAttrs(context, attrs);
        init();
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void init() {
        View view = RelativeLayout.inflate(mContext, R.layout.view_timerecharge, null);
        //以下两句的顺序不能调换，要先addView，然后才能通过findViewById找到该TextView
        addView(view);
        ButterKnife.bind(this);
        getView(mTime,0);
    }

    /**
     * iv1
     */
    @OnClick(R.id.view_timertcharge_ll1)
    void ll1(){
        getView(1,0);
    }
    /**
     * iv2
     */
    @OnClick(R.id.view_timertcharge_ll2)
    void ll2(){
        getView(2,0);
    }
    /**
     * iv3
     */
    @OnClick(R.id.view_timertcharge_ll3)
    void ll3(){
        getView(3,0);

    }
    /**
     * iv4
     */
    @OnClick(R.id.view_timertcharge_ll4)
    void ll4(){
        getView(4,0);
    }

    /**
     * 更新部分 视图  type 0 代表为本activity 1代表其他
     */
    public void getView(int timw,int type){
        LogUtil.eE("",".....getView...."+timw);
        switch (timw){
            case 1:
                mImageView1.setBackgroundResource(R.drawable.view_timerecharg_true);
                mImageView2.setBackgroundResource(R.drawable.view_timerecharg_false);
                mImageView3.setBackgroundResource(R.drawable.view_timerecharg_false);
                mImageView4.setBackgroundResource(R.drawable.view_timerecharg_false);
                mView1.setBackgroundResource(R.color.binding_gray);
                mView2.setBackgroundResource(R.color.binding_gray);
                mView3.setBackgroundResource(R.color.binding_gray);
                break;
            case 2:
                mImageView1.setBackgroundResource(R.drawable.view_timerecharg_true);
                mImageView2.setBackgroundResource(R.drawable.view_timerecharg_true);
                mImageView3.setBackgroundResource(R.drawable.view_timerecharg_false);
                mImageView4.setBackgroundResource(R.drawable.view_timerecharg_false);
                mView1.setBackgroundResource(R.color.repain_blue);
                mView2.setBackgroundResource(R.color.binding_gray);
                mView3.setBackgroundResource(R.color.binding_gray);
                break;
            case 3:
                mImageView1.setBackgroundResource(R.drawable.view_timerecharg_true);
                mImageView2.setBackgroundResource(R.drawable.view_timerecharg_true);
                mImageView3.setBackgroundResource(R.drawable.view_timerecharg_true);
                mImageView4.setBackgroundResource(R.drawable.view_timerecharg_false);
                mView1.setBackgroundResource(R.color.repain_blue);
                mView2.setBackgroundResource(R.color.repain_blue);
                mView3.setBackgroundResource(R.color.binding_gray);
                break;
            case 4:
                mImageView1.setBackgroundResource(R.drawable.view_timerecharg_true);
                mImageView2.setBackgroundResource(R.drawable.view_timerecharg_true);
                mImageView3.setBackgroundResource(R.drawable.view_timerecharg_true);
                mImageView4.setBackgroundResource(R.drawable.view_timerecharg_true);
                mView1.setBackgroundResource(R.color.repain_blue);
                mView2.setBackgroundResource(R.color.repain_blue);
                mView3.setBackgroundResource(R.color.repain_blue);
                break;
            case 0:
                mImageView1.setBackgroundResource(R.drawable.view_timerecharg_false);
                mImageView2.setBackgroundResource(R.drawable.view_timerecharg_false);
                mImageView3.setBackgroundResource(R.drawable.view_timerecharg_false);
                mImageView4.setBackgroundResource(R.drawable.view_timerecharg_false);
                mView1.setBackgroundResource(R.color.binding_gray);
                mView2.setBackgroundResource(R.color.binding_gray);
                mView3.setBackgroundResource(R.color.binding_gray);
                break;
        }

        if(type == 0){
            if(mViewTimeRechargeOnClick !=null){
                mViewTimeRechargeOnClick.onItemClick(timw);
            }
        }
    }

    /**
     * get attrs
     */
    private void getAttrs(Context context, AttributeSet attrs) {
        TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.view_time_recharge);
        String time = ta.getString(R.styleable.view_time_recharge_time);
        mTime = Integer.parseInt(time);
        ta.recycle();
    }


    public interface viewTimeRechargeOnClick {
        void onItemClick(int position);
    }

    public void setViewTimeRechargeOnClick(viewTimeRechargeOnClick viewTimeRechargeOnClick){
        this.mViewTimeRechargeOnClick = viewTimeRechargeOnClick;
    }


}
